<?php
/**
 * Mechanism
 *
 * LICENSE
 *
 * Copyright (c) 2011, Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * * Neither the name of the  nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package Mechanism
 * @license http://www.opensource.org/licenses/bsd-license
 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * @copyright Copyright (c) 2011 Sergey A. Ivonchik
 */

namespace Mechanism\Router {
	use Mechanism\Router\Route\IRoute;
	/**
	 * Complex router
	 *
	 * This router can route several ways.
	 *
 	 * @package Mechanism\Router
 	 * @author BreatheInMyVoid <BreatheInMyVoid@gmail.com>
	 */
	class Complex extends Router {
		/**
		 * Routes for matching
		 * 
		 * @var array $routes
		 */
		protected $routes = array( );
		
		/**
		 * Constructor
		 * 
		 * @throws Mechanism\Router\Exception\Invalid
		 * @param array $routes
		 */
		public function __construct( array $routes = null ) {
			if ( null !== $routes ) {
				foreach ( $routes as $route ) {
					if ( $route instanceof IRoute ) {
						array_push( $this->routes, $route );
					} else {
						throw new Exception\Invalid( 
							'Invalid router params. Routes must implement IRoute.'
						); 
					}
				}
			}
		}
		
		/**
		 * Push route in stack for matching
		 * 
		 * @param Mechanism\Router\Route\IRoute $route
		 * @return Mechanism\Router\Complex $this
		 */
		public function pushRoute( IRoute $route ) {
			array_push( $this->routes, $route );
			return $this;
		}
		
		/** 
		 * Peek route
		 * 
		 * Returns last pushed route form stack of 
		 * matching routes.
		 * 
		 * @return Mechanism\Router\Route\IRoute $route
		 */
		public function peekRoute( ) {
			$last = count( $this->routes ) - 1;
			
			if( isset( $this->routes[$last] ) && !empty( $this->routes[$last] ) ) {
				return $this->routes[$last];
			} else {
				return null;
			}
		}

		/** 
		 * Pop route
		 * 
		 * Returns last pushed route form stack of 
		 * matching routes and removes it from stack.
		 * 
		 * @return Mechanism\Router\Route\IRoute $route
		 */
		public function popRoute( ) {
			return array_pop( $this->routes );		
		} 
		
		/**
		 * Do routing
		 * 
		 * @throws Mechanism\Router\Exception\Runtime
		 * @param string $path
		 * @return bool $flag
		 */
		public function routing( $path ) {
			if ( empty( $this->routes ) ) {
				throw new Exception\Runtime( 'Routes was not setted' );
			} 			
			
			$i = count( $this->routes ) - 1;
			$flag = false;
			
			for ( $i; $i >= 0; --$i ) {
				if ( $this->routes[$i]->match( $path ) ) {
					$this->matchedRoute = $this->routes[$i];
					return true;					
				}
			}
			
			$this->matchedRoute = null;
			return false;
		}
	}
}